#include "GamePlayer.h"

GamePlayer::GamePlayer()
{
	m_moveDir = 0;
	m_moveSpd = 0.0f;
	m_aniTime = 0.025f;
}

GamePlayer::~GamePlayer()
{

}

void GamePlayer::initPlayer(float a_moveSpd, float a_anglSpd)
{
	m_moveSpd = a_moveSpd;
	m_anglSpd = a_anglSpd;
}

void GamePlayer::input()
{
	if(m_core->keyInput(KEY_W) && m_core->keyInput(KEY_A))
		m_moveDir = DIR_UPLT; 
	else if(m_core->keyInput(KEY_W) && m_core->keyInput(KEY_D))
		m_moveDir = DIR_UPRT;
	else if(m_core->keyInput(KEY_S) && m_core->keyInput(KEY_A))
		m_moveDir = DIR_DNLT;
	else if(m_core->keyInput(KEY_S) && m_core->keyInput(KEY_D))
		m_moveDir = DIR_DNRT;
	else if(m_core->keyInput(KEY_W))
		m_moveDir = DIR_UP;
	else if(m_core->keyInput(KEY_A))
		m_moveDir = DIR_LT;
	else if(m_core->keyInput(KEY_S))
		m_moveDir = DIR_DN;
	else if(m_core->keyInput(KEY_D))
		m_moveDir = DIR_RT;

	if(m_core->keyPress(KEY_UP))
		m_body->m_pos += V2DF(0,-32.0f);
	else if(m_core->keyPress(KEY_LEFT))
		m_body->m_pos += V2DF(-32.0f,0);
	else if(m_core->keyPress(KEY_DOWN))
		m_body->m_pos += V2DF(0,32.0f);
	else if(m_core->keyPress(KEY_RIGHT))
		m_body->m_pos += V2DF(32.0f,0);

	if(!m_core->keyInput(KEY_W)
		&& !m_core->keyInput(KEY_A)
		&& !m_core->keyInput(KEY_S)
		&& !m_core->keyInput(KEY_D))
		m_moveDir = DIR_NONE;
}

void GamePlayer::update()
{
	switch(m_moveDir)
	{
	case DIR_NONE:
		m_body->m_lnFrc = V2D_ZERO;
		m_body->m_agFrc = 0.0f;
		break;
	case DIR_UP:
		m_body->m_lnFrc += V2DF(0,-m_moveSpd);
		m_body->m_rot = 270.0f;
		break;
	case DIR_LT:
		m_body->m_lnFrc += V2DF(-m_moveSpd,0);
		m_body->m_rot = 180.0f;
		break;
	case DIR_DN:
		m_body->m_lnFrc += V2DF(0,m_moveSpd);
		m_body->m_rot = 90.0f;
		break;
	case DIR_RT:
		m_body->m_lnFrc += V2DF(m_moveSpd,0);
		m_body->m_rot = 0.0f;
		break;
	case DIR_UPLT:
		m_body->m_lnFrc += V2DF(-1,-1).normal() * m_moveSpd;
		m_body->m_rot = 225.0f;
		break;
	case DIR_UPRT:
		m_body->m_lnFrc += V2DF(1,-1).normal() * m_moveSpd;
		m_body->m_rot = 315.0f;
		break;
	case DIR_DNLT:
		m_body->m_lnFrc += V2DF(-1,1).normal() * m_moveSpd;
		m_body->m_rot = 135.0f;
		break;
	case DIR_DNRT:
		m_body->m_lnFrc += V2DF(1,1).normal() * m_moveSpd;
		m_body->m_rot = 45.0f;
		break;
	}

	if(m_moveDir != DIR_NONE)
		aniPlay();
	else
	{
		float hor = ((float)m_texture.m_width)/m_aniCellHor;
		float ver = ((float)m_texture.m_height)/m_aniCellVer;

		m_aniCol = 0;
		m_aniRow = 1;
		m_texture.m_texMin.x = 0;
		m_texture.m_texMax.x = hor;
		m_texture.m_texMin.y = ver;
		m_texture.m_texMax.y = ver*2.0f;
	}

	GameTexture::m_pos = GameBody::m_body->m_pos;
	GameTexture::m_rot = GameBody::m_body->m_rot;
	GameTexture::update();
}